home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.07 Jul 88 / Window Sample Stuff / Window.Asm < prev    next >
Encoding:
Assembly Source File  |  1988-03-28  |  25.5 KB  |  773 lines  |  [TEXT/EDIT]

  1. ; File Window.Asm
  2. ;-------------------------------------------------------------------------
  3. ;       Macintosh 68000 Development System -- Programming Example
  4. ;-------------------------------------------------------------------------
  5. ; The Window Sample Program issued with the MDS.
  6. ; Modified by Ray.A.Cameron to demonstate a Modal Dialog Box.
  7. ; Mon May 4, 1987 21:29:54
  8.  
  9. ; This application displays a window within which you can enter and edit
  10. ; text. Program control is through four menus: the Apple menu, the File 
  11. ; menu, the Edit menu, and the Font menu.
  12.  
  13. ; The Apple menu has the standard desk accessories and an About feature.
  14.  
  15. ; The File menu lets you quit the application.     
  16.  
  17. ; The Edit menu lets you cut, copy, paste, and clear the text in the window
  18. ; or in the desk accessories.  Undo is provided for desk accesories only.
  19. ; Command key equivalents for undo, cut, copy, and paste are provided.
  20. ; Cutting and pasting between the application and the desk accessories is 
  21. ; not supported.  This requires use of the Scrap Manager.
  22.  
  23. ; The Font menu lets you display a Modal Dialog Box to select a Font
  24. ; Name, Size and attributes.
  25.  
  26. ; This program requires the use of a resource file called "Window.Rsrc"   
  27. ; Window.Rsrc is created from "Window.R" using RMaker. As well as
  28. ; the two files "SetupFontMap" and "FontDialog".
  29.  
  30. ;--------------------------------- Includes -------------------------------
  31.  
  32. Include             Traps.D                    ; Use System and ToolBox traps
  33. Include             ToolEqu.D                    ; Use ToolBox equates
  34. Include                QuickEqu.D                ; Use QuickDraw equates
  35. Include                SysEqu.D                    ; Use System equates
  36. Include                PackMacs.Txt                ; Use Package equates
  37.  
  38. ;----------------------------- Use of Registers ---------------------------
  39.  
  40. ; Operating System and Toolbox calls always preserve D3-D7, and A2-A4.
  41.  
  42. ; Register use: A5-A7 are reserved by the system
  43. ;          D1-D3, A0-A1 are unused
  44. ;          D0 is used as a temp
  45.  
  46. ModifyReg        Equ        D4         ; D4 holds modifier bits from GetNextEvent 
  47. MenuReg        Equ        D5        ; D5 holds menu ID from MenuSelect,MenuKey
  48. MenuItemReg    Equ        D6        ; D6 holds item ID from MenuSelect,MenuKey
  49. AppleHReg        Equ        D7        ; D7 holds the handle to the Apple Menu
  50.  
  51. TextHReg        Equ        A2        ; A2 is a handle to the TextEdit record
  52. WindowPReg    Equ        A3        ; A3 is a pointer to the editing window
  53. EditHReg        Equ        A4        ; A4 is a handle to the Edit menu
  54.  
  55. ;--------------------------------- Equates --------------------------------
  56.  
  57. ; These are equates associated with the resources
  58. ; for the Window example.
  59.  
  60. AppleMenu     Equ        1        ; First item in MENU resource
  61.   AboutItem        Equ        1        ; First item in Apple menu
  62.  
  63. FileMenu        Equ        2        ; Second item in MENU resource
  64.   QuitItem         Equ        1        ; First item in File menu
  65.  
  66. EditMenu        Equ        3        ; Third item in MENU resource
  67.   UndoItem        Equ        1        ; Items in Edit menu
  68.   CutItem        Equ        3        ; (Item 2 is a line)
  69.   CopyItem        Equ        4
  70.   PasteItem        Equ        5
  71.   ClearItem        Equ        6
  72.   
  73. FontMenu        Equ        4        ; Fourth item in MENU resource
  74.   DialogItem    Equ        1        ; Only item in Font menu
  75.  
  76. AboutDialog    Equ        1        ; About dialog is DLOG resource #1
  77. ButtonItem        Equ        1        ; First item in DITL used by DLOG #1
  78. ASample        Equ        1        ; Sample Window is WIND resource #1
  79.  
  80. ;---------------------------------- Xdefs ---------------------------------
  81.  
  82. ; Xdef all labels that are to be symbolically displayed by debugger.
  83.  
  84.     Xdef        Start
  85.     Xdef        InitManagers
  86.     Xdef        SetupMenu
  87.     Xdef        SetupWindow
  88.     Xdef        SetupTextEdit
  89.     Xdef        Activate
  90.     Xdef        Deactivate
  91.     Xdef        Update
  92.     Xdef        KeyDown
  93.     Xdef        MouseDown
  94.     Xdef        SystemEvent
  95.     Xdef        Content
  96.     Xdef        Drag
  97.     Xdef        InMenu
  98.     Xdef        About
  99.     
  100.     XRef        SetupFontMap            ; Routines
  101.     XRef        FontDialog
  102.     
  103.     XRef        FontMap                ; Variable
  104.     
  105. ;------------------------------- Main Program -----------------------------
  106.  
  107. Start    
  108.  
  109.     Bsr            InitManagers            ; Initialize managers
  110.     Bsr            SetupMenu                ; Build menus, draw menu bar
  111.     Bsr            SetupWindow            ; Draw Editing Window
  112.     Bsr            SetupTextEdit            ; Initialize TextEdit
  113.     Bsr            SetupFontMap            ; Initialize the FontMap
  114.     
  115. EventLoop                                    ; Main Program Loop
  116.  
  117.     _SystemTask                            ; Update Desk Accessories
  118.     ; Procedure    TEIdle (hTE:TEHandle);
  119.     Move.l            TextHReg,-(SP)        ; Get handle to text record
  120.     _TEIdle                                ; blink cursor etc.
  121.         
  122.     ; Function    GetNextEvent(eventMask: Integer
  123.     ;        Var theEvent: EventRecord) : Boolean
  124.     Clr                 -(SP)                    ; Clear space for result
  125.     Move             #$0FFF,-(SP)            ; Allow 12 low events
  126.     Pea            EventRecord            ; Place to return results
  127.     _GetNextEvent                        ; Look for an event
  128.     Move            (SP)+,D0                ; Get result code
  129.     Beq              EventLoop                ; No event... Keep waiting
  130.     Bsr             HandleEvent            ; Go handle event
  131.     Beq            EventLoop                ; Not Quit, keep going
  132.     Rts                                        ; Quit, exit to Finder
  133.  
  134. ; Note: When an event handler finishes, it returns the Z flag set.  If 
  135. ;    Quit was selected, it returns with the Z flag clear.  An Rts is
  136. ;    guaranteed to close all files and launch the Finder.
  137.     
  138. ;------------------------------- InitManagers -----------------------------
  139.  
  140. InitManagers
  141.  
  142.     Pea            -4(A5)                    ; Quickdraw's global area
  143.     _InitGraf                                ; Init Quickdraw
  144.     _InitFonts                                ; Init Font Manager
  145.     Move.l            #$0000FFFF,D0        ; Flush all events
  146.     _FlushEvents
  147.     _InitWindows                            ; Init Window Manager
  148.     _InitMenus                                ; Init Menu Manager
  149.     Clr.l    -(SP)                            ; No restart Procedure
  150.     _InitDialogs                            ; Init Dialog Manager
  151.     _TEInit                                    ; Init Text Edit
  152.     _InitCursor                                ; Turn on arrow cursor
  153.     Rts
  154.  
  155. ;------------------------------- SetupMenu ------------------------------
  156.  
  157. SetupMenu
  158.  
  159. ; The names of all the menus and the commands in the menus are stored in the
  160. ; resource file.  The way you build a menu for an application is by reading
  161. ; each menu in from the resource file and then inserting it into the current
  162. ; menu bar.  Desk accessories are read from the system resource file and
  163. ; added to the Apple menu.
  164.  
  165. ; Apple Menu Set Up.  
  166.  
  167.     ; Function    GetMenu (menu ID:Integer): MenuHandle;
  168.     Clr.l            -(SP)                    ; Space for menu handle
  169.     Move             #AppleMenu,-(SP)        ; Apple menu resource ID 
  170.     _GetRMenu                            ; Get menu handle 
  171.     Move.l            (SP),AppleHReg        ; Save for later comparison
  172.     Move.l            (SP),-(SP)                ; Copy handle for AddResMenu
  173.     
  174.     ; Procedure    InsertMenu (menu:MenuHandle; beforeID: Integer);
  175.     Clr                 -(SP)                    ; Append to menu
  176.     _InsertMenu                            ; Which is currently empty
  177.  
  178. ; Add Desk Accessories Into Apple menu (Apple menu handle already on stack)
  179.  
  180.     ; Procedure    AddResMenu (menu: MenuHandle; theType: ResType);
  181.     Move.l            #'DRVR',-(SP)            ; Load all drivers 
  182.     _AddResMenu                            ; And Add to Apple menu
  183.  
  184. ; File Menu Set Up
  185.  
  186.     ; Function    GetMenu (menu ID:Integer): MenuHandle;
  187.     Clr.l            -(SP)                    ; Space for menu handle
  188.     Move             #FileMenu,-(SP)        ; File Menu Resource ID 
  189.     _GetRMenu                            ; Get File menu handle
  190.  
  191.     ; Procedure    InsertMenu (menu:MenuHandle; beforeID: Integer);
  192.     Clr                 -(SP)                    ; Append to list
  193.     _InsertMenu                            ; After Apple menu
  194.  
  195. ; Edit Menu Set Up
  196.  
  197.     ; Function    GetMenu (menu ID:Integer): MenuHandle;
  198.     Clr.l            -(SP)                    ; Space for menu handle
  199.     Move             #EditMenu,-(SP)        ; Edit menu resource ID
  200.     _GetRMenu                            ; Get handle to menu
  201.     Move.l            (SP),EditHReg            ; Save for later
  202.                                                     ; Leave on stack for Insert
  203.     ; Procedure    InsertMenu (menu:MenuHandle; beforeID: Integer);
  204.     Clr                 -(SP)                    ; Append to list
  205.     _InsertMenu                            ; After File menu
  206.     
  207. ; Font Menu Set Up
  208.  
  209.     ; Function    GetMenu (menu ID:Integer): MenuHandle;
  210.     Clr.l            -(SP)                    ; Space for menu handle
  211.     Move             #FontMenu,-(SP)        ; Font Menu Resource ID 
  212.     _GetRMenu                            ; Get File menu handle
  213.  
  214.     ; Procedure    InsertMenu (menu:MenuHandle; beforeID: Integer);
  215.     Clr                 -(SP)                    ; Append to list
  216.     _InsertMenu                            ; After Edit menu
  217.     _DrawMenuBar                            ; Display the menu bar
  218.     Rts
  219.  
  220. ;------------------------------ SetupWindow ------------------------------
  221.  
  222. SetupWindow
  223.  
  224. ; The window parameters are stored in our resource file.  Read them from 
  225. ; the file and draw the window, then set the port to that window.  Note that
  226. ; the window parameters could just as easily have been set using the call
  227. ; NewWindow, which doesn't use the resource file.
  228.  
  229.     ; Function    GetNewWindow (windowID: Integer; wStorage: Ptr; 
  230.     ;                  behind: WindowPtr) : WindowPtr;
  231.     Clr.l            -(SP)                    ; Space for window pointer
  232.     Move             #ASample,-(SP)        ; Resource ID for window
  233.     Pea            WindowStorage(A5)    ; Storage for window
  234.     Move.l            #-1,-(SP)                ; Make it the top window
  235.     _GetNewWindow                        ; Draw the window
  236.     Move.l            (SP),WindowPReg        ; Save for later 
  237.     
  238.     ; Procedure    SetPort (gp: GrafPort)    ; Pointer still on stack
  239.     _SetPort                                ; Make it the current port
  240.     Rts
  241.     
  242. ;---------------------------- SetupTextEdit --------------------------
  243.  
  244. SetupTextEdit
  245.  
  246. ; Create a new text record for TextEdit, and define the window within which
  247. ; it will be displayed.  Note that if the window boundaries are changed in
  248. ; the resource file, DestRect and ViewRect will have to be changed too.
  249.  
  250.     ; Procedure    TENew (destRect,viewRect: Rect): TEHandle;
  251.     Clr.l            -(SP)                    ; Space for text handle 
  252.     Pea            DestRect                ; DestRect Rectangle
  253.     Pea            ViewRect                ; ViewRect Rectangle
  254.     _TENew                                ; New Text Record
  255.     Move.l            (SP)+,TextHReg        ; Save text handle
  256.     Rts
  257.     
  258. ;------------------------- Event Handling Routines -----------------------
  259.  
  260. HandleEvent
  261.  
  262. ; Use the event number as an index into the Event table.  These 12 events
  263. ; are all the things that could spontaneously happen while the program is
  264. ; in the main loop.
  265.  
  266.     Move            Modify,ModifyReg        ; More useful in a reg
  267.     Move            What,D0                ; Get event number
  268.     Add            D0,D0                    ; *2 for table index
  269.     Move            EventTable(D0),D0        ; Point to routine offset
  270.     Jmp            EventTable(D0)        ; and jump to it
  271.  
  272. EventTable    
  273.  
  274.     Dc.w        NextEvent-EventTable        ; Null Event (Not used)
  275.     Dc.w        MouseDown-EventTable    ; Mouse Down
  276.     Dc.w        NextEvent-EventTable        ; Mouse Up (Not used)
  277.     Dc.w        KeyDown-EventTable        ; Key Down
  278.     Dc.w        NextEvent-EventTable        ; Key Up (Not used)
  279.     Dc.w        KeyDown-EventTable        ; Auto Key
  280.     Dc.w        Update-EventTable            ; Update 
  281.     Dc.w        NextEvent-EventTable        ; Disk (Not used) 
  282.     Dc.w        Activate-EventTable        ; Activate
  283.     Dc.w        NextEvent-EventTable        ; Abort (Not used)
  284.     Dc.w        NextEvent-EventTable        ; Network (Not used)
  285.     Dc.w        NextEvent-EventTable        ; I/O Driver (Not used)
  286.     
  287. ;--------------------------- Event Actions ---------------------------
  288.  
  289. Activate        
  290.  
  291. ; An activate event is posted by the system when a window needs to be
  292. ; activated or deactivated.  The information that indicates which window
  293. ; needs to be updated was returned by the NextEvent call.
  294.  
  295.     Cmp.l            Message,WindowPReg    ; Was it our window?
  296.     Bne             NextEvent                ; No, get next event
  297.     Btst            #activeFlag,ModifyReg    ; Activate?
  298.     Beq             Deactivate                ; No, go do Deactivate
  299.  
  300. ; To activate our window, activate TextEdit, and disable Undo since we don't
  301. ; support it.  Then set our window as the port since an accessory may have
  302. ; changed it.  This activate event was generated by SelectWindow as a result
  303. ; of a click in the content region of our window.  If the window had scroll
  304. ; bars, we would do ShowControl and HideControl here too.
  305.  
  306.     ; Procedure    TEActivate (hTE: TEHandle);
  307.     Move.l            TextHReg,-(SP)        ; Move Text Handle To Stack
  308.     _TEActivate                            ; Activate Text
  309.  
  310.     ; Procedure DisableItem (menu:MenuHandle; item:Integer);
  311.     Move.l            EditHReg,-(SP)            ; Get handle to the menu
  312.     Move            #UndoItem,-(SP)        ; Enable 1st item (undo)
  313.     _DisableItem
  314.  
  315. SetOurPort                                    ; used by InAppleMenu
  316.  
  317.     ; Procedure     SetPort (gp: GraphPort) ; Set the port to us, since
  318.     Move.l            WindowPReg,-(SP)        ; an accessory might have
  319.     _SetPort                                ; changed it.
  320.  
  321. NextEvent    
  322.  
  323.     Moveq #0,D0                            ; Say that it's not Quit
  324.     Rts                                        ; return to EventLoop
  325.         
  326. Deactivate
  327.  
  328. ; To deactivate our window, turn off TextEdit, and Enable undo for the desk
  329. ; accessories (which must be active instead of us).
  330.  
  331.     ; Procedure    TEDeActivate (hTE: TEHandle)
  332.     Move.l            TextHReg,-(SP)        ; Get Text Handle
  333.     _TeDeActivate                            ; Un Activate Text
  334.  
  335.     ; Procedure EnableItem (menu:MenuHandle; item:Integer);
  336.     Move.l            EditHReg,-(SP)            ; Get handle to the menu
  337.     Move            #UndoItem,-(SP)        ; Enable 1st item (undo)
  338.     _EnableItem
  339.     Bra             NextEvent                ; Go get next event
  340.     
  341. Update    
  342.  
  343. ; The window needs to be redrawn.  Erase the window and then call TextEdit
  344. ; to redraw it.
  345.  
  346.     ; Procedure    BeginUpdate (theWindow: WindowPtr);
  347.     Move.l            WindowPReg,-(SP)        ; Get pointer to window
  348.     _BeginUpDate                            ; Begin the update
  349.  
  350.     ; EraseRect (rUpdate: Rect);
  351.     Pea            ViewRect                ; Erase visible area
  352.     _EraseRect
  353.     
  354.     ; TEUpdate (rUpdate: Rect; hTE: TEHandle);
  355.     Pea            ViewRect                ; Get visible area
  356.     Move.l            TextHReg,-(SP)        ; and handle to text 
  357.     _TEUpdate                                ; then update the window
  358.  
  359.     ; Procedure    EndUpdate (theWindow: WindowPtr);
  360.     Move.l            WindowPReg,-(SP)        ; Get pointer to window
  361.     _EndUpdate                            ; and end the update
  362.     Bra             NextEvent                ; Go get next event
  363.     
  364. KeyDown    
  365.  
  366. ; A key was pressed.  First check to see if it was a command key.  If so, 
  367. ; go do it.  Otherwise pass the key to TextEdit.
  368.  
  369.     Btst            #CmdKey,ModifyReg    ; Is command key down?
  370.     Bne             CommandDown            ; If so, handle command key
  371.  
  372.     ; Procedure    TEKey (key: CHAR; hTE: TEHandle);
  373.     Move             Message+2,-(SP)        ; Get character
  374.     Move.l            TextHReg,-(SP)        ; and text record
  375.     _TEKey                                ; Give char to TextEdit
  376.     Bra             NextEvent                ; Go get next event
  377.     
  378. CommandDown    
  379.  
  380. ; The command key was down.  Call MenuKey to find out if it was the command 
  381. ; key equivalent for a menu command, pass the menu and item numbers to Choices.
  382.  
  383.     ; Function    MenuKey (ch:CHAR): LongInt;
  384.     Clr.l            -(SP)                    ; Space for Menu and Item
  385.     Move             Message+2,-(SP)        ; Get character
  386.     _MenuKey                                ; See if it's a command
  387.     Move            (SP)+,MenuReg        ; Save Menu
  388.     Move            (SP)+,MenuItemReg    ; and Menu Item
  389.     Bra             Choices                ; Go dispatch command
  390.  
  391.  
  392. ;--------------------Mouse Down Events And Their Actions----------------------
  393.  
  394. MouseDown    
  395.  
  396. ; If the mouse button was pressed, we must determine where the click
  397. ; occurred before we can do anything.  Call FindWindow to determine
  398. ; where the click was; dispatch the event according to the result.
  399.  
  400.     ; Function    FindWindow (thePt: Point; 
  401.     ;                    Var whichWindow: WindowPtr): Integer;
  402.     Clr                 -(SP)                    ; Space for result
  403.     Move.l            Point,-(SP)                ; Get mouse coordinates
  404.     Pea            WWindow                ; Event Window
  405.     _FindWindow                            ; Who's got the click? 
  406.     Move             (SP)+,D0                ; Get region number
  407.     Add            D0,D0                    ; *2 for index into table
  408.     Move            WindowTable(D0),D0    ; Point to routine offset
  409.     Jmp            WindowTable(D0)        ; Jump to routine
  410.     
  411. WindowTable
  412.  
  413.     Dc.w        NextEvent-WindowTable    ; In Desk (Not used)
  414.     Dc.w        InMenu-WindowTable        ; In Menu Bar
  415.     Dc.w        SystemEvent-WindowTable ; System Window
  416.     Dc.w        Content-WindowTable        ; In Content
  417.     Dc.w        Drag-WindowTable            ; In Drag
  418.     Dc.w        NextEvent-WindowTable    ; In Grow (Not used)
  419.     Dc.w        NextEvent-WindowTable    ; In Go Away (Not used)
  420.  
  421. SystemEvent    
  422.  
  423. ; The mouse button was pressed in a system window.  SystemClick calls the 
  424. ; appropriate desk accessory to handle the event.
  425.  
  426.     ; Procedure    SystemClick (theEvent: EventRecord;
  427.     ;                     theWindow: WindowPtr);
  428.     Pea            EventRecord            ; Get event record
  429.     Move.l            WWindow,-(SP)        ; and window pointer
  430.     _SystemClick                            ; Let the system do it
  431.     Bra             NextEvent                ; Go get next event
  432.     
  433. Content    
  434.  
  435. ; The click was in the content area of a window.  If our window was in 
  436. ; front, then call Quickdraw to get local coordinates, then pass the 
  437. ; coordinates to TextEdit. We also determine whether the shift key was 
  438. ; pressed so TextEdit can do shift-clicking. If our window wasn't in 
  439. ; front, Move it to the front, but don't process click.
  440.  
  441.     Clr.l            -(SP)                    ; clear room for result
  442.     _FrontWindow                            ; get FrontWindow
  443.     Move.l            (SP)+,D0                ; Is front window pointer
  444.     Cmp.l            WindowPReg,D0        ; same as our pointer?
  445.     Beq.s             @1                        ; Yes, call TextEdit
  446.  
  447. ; We weren't active, select our window.  This causes an activate event.
  448.  
  449.     ; Procedure    SelectWindow (theWindow: WindowPtr);
  450.     Move.l            WWindow,-(SP)        ; Window Pointer To Stack
  451.     _SelectWindow                            ; Select Window
  452.     Bra             NextEvent                ; and get next event
  453.  
  454. @1    
  455.  
  456. ; We were active, pass the click (with shift) to TextEdit.
  457.  
  458.     ; Procedure    GlobalToLocal (Var pt:Point);
  459.     Pea            Point                    ; Mouse Point
  460.     _GlobalToLocal                        ; Global To Local
  461.  
  462.     ; Procedure     TEClick (pt: Point; extend: Boolean; hTE: TEHandle);
  463.     Move.l            Point,-(SP)                ; Mouse Point (GTL)
  464.     Btst            #shiftKey,ModifyReg    ; Is shift key down?
  465.     Sne            D0                        ; True if shift down
  466.     
  467. ; Note:  We want the Boolean in the high byte, so use Move.b.  The 68000
  468. ; pushes an extra, unused byte on the stack for us.
  469.  
  470.     Move.b            D0,-(SP)        
  471.     Move.l            TextHReg,-(SP)        ; Identify Text
  472.     _TEClick                                ; TEClick
  473.     Bra             NextEvent                ; Go get next event
  474.     
  475. Drag
  476.  
  477. ; The click was in the drag bar of the window.  Draggit.
  478.  
  479.     ; DragWindow (theWindow:WindowPtr; startPt: Point; boundsRect: Rect);
  480.     Move.l            WWindow,-(SP)        ; Pass window pointer
  481.     Move.l            Point,-(SP)                ; mouse coordinates
  482.     Pea            WBounds                ; and boundaries
  483.     _DragWindow                            ; Drag Window
  484.     Bra             NextEvent                ; Go get next event
  485.     
  486. InMenu    
  487.  
  488. ; The click was in the menu bar.  Determine which menu was selected, then
  489. ; call the appropriate routine.
  490.  
  491.     ; Function    MenuSelect (startPt:Point) : LongInt;
  492.     Clr.l            -(SP)                    ; Get Space For Menu Choice
  493.     Move.l            Point,-(SP)                ; Mouse At Time Of Event
  494.     _MenuSelect                            ; Menu Select
  495.     Move             (SP)+,MenuReg        ; Save Menu
  496.     Move            (SP)+,MenuItemReg    ; and Menu Item
  497.  
  498. ; On entry to Choices, the resource ID of the Menu is saved in the low
  499. ; word of a register, and the resource ID of the MenuItem in another.
  500. ; The routine MenuKey, used when a command key is pressed, returns the same 
  501. ; info.
  502.  
  503. Choices                                    ; Called by command key too
  504.  
  505.     Cmp             #AppleMenu,MenuReg    ; Is It In Apple Menu?
  506.     Beq             InAppleMenu            ; Go do Apple Menu
  507.     Cmp             #FileMenu,MenuReg    ; Is It In File Menu?
  508.     Beq             InFileMenu                ; Go do File Menu
  509.     Cmp             #EditMenu,MenuReg    ; Is It In Edit Menu?
  510.     Beq             InEditMenu                ; Go do Edit Menu
  511.     Cmp             #FontMenu,MenuReg    ; Is It In Font Menu?
  512.     Beq             InFontMenu            ; Go do Font Menu
  513.     
  514. ChoiceReturn
  515.  
  516.     Bsr                UnHiliteMenu            ; Unhighlight the menu bar
  517.     Bra             NextEvent                ; Go get next event
  518.         
  519. InFileMenu
  520.  
  521. ; If it was in the File menu, just check for Quit since that's all there is.
  522.  
  523.     Cmp            #QuitItem,MenuItemReg    ; Is It Quit?
  524.     Bne             ChoiceReturn            ; No, Go get next event
  525.     Bsr                UnHiliteMenu            ; Unhighlight the menu bar
  526.     Move            #-1,D0                    ; say it was Quit
  527.     Rts
  528.     
  529. InEditMenu
  530.  
  531. ; First, call SystemEdit.  If a desk accessory is active that uses the Edit
  532. ; menu (such as the Notepad) this lets it use our menu.
  533. ; Decide whether it was cut, copy, paste, or clear.  Ignore Undo since we
  534. ; didn't implement it.
  535.  
  536.     Bsr                SystemEdit            ; Desk accessory active?
  537.     Bne             ChoiceReturn            ; Yes, SystemEdit handled it
  538.     Cmp            #CutItem,MenuItemReg    ; Is It Cut?
  539.     Beq             Cut                        ; Yes, go handle it
  540.     Cmp            #CopyItem,MenuItemReg    ; Is it Copy?
  541.     Beq             Copy                    ; Yes, go handle it
  542.     Cmp            #PasteItem,MenuItemReg    ; Is it Paste?
  543.     Beq             Paste                    ; Yes, go handle it
  544.     Cmp            #ClearItem,MenuItemReg    ; Is it Clear?
  545.     Beq             ClearIt                    ; Yes, go handle it
  546.     Bra             ChoiceReturn            ; Go get next event
  547.     
  548. InFontMenu
  549.  
  550. ; If it was in the Font menu, just check for Dialog since that's all there is.
  551.  
  552.     Cmp            #DialogItem,MenuItemReg    ; Is It Dialog?
  553.     Bne             ChoiceReturn                ; No, Go get next event
  554.     
  555.     ; Procedure    TEDeActivate (hTE: TEHandle)
  556.     Move.l            TextHReg,-(SP)        ; Identify Text
  557.     _TEDeActivate                            ; Deactivate Text
  558.     
  559. ; Obtain the current Font number, size and attributes to setup the dialog box.
  560.     Movea.l        TextHReg,A0            ; Handle to the TE record
  561.     Movea.l        (A0),A0                ; Pointer to the TE record
  562.     Move.w        teFont(A0),D0            ;- Font Number
  563.     Cmp.w            #1,D0                    ;     Default number?
  564.     Bne.s            @0                        ;     No, -> @0
  565.     Move.w        ApFontID,D0            ;     Yes, load default number
  566. @0    Move.w        D0,DtxFont(A5)
  567.     Clr.w            D0                        ;- Font Face
  568.     Move.b            teFace(A0),D0            ;     Place the attribute bits in the
  569.     Move.w        D0,DtxFace(A5)        ;     low byte of DtxFace(A5).
  570.     Move.w        teSize(A0),D0            ;- Font Size
  571.     Bne.s            @1                        ;     Default size, No -> @1
  572.     Move.b            FMDefaultSize,D0        ;     Yes, load default size
  573. @1    Move.w        D0,DtxSize(A5)
  574.     
  575.     ; Function FontDialog (Var teFont: Integer; Var teFace: Style;
  576.     ;                    Var teSize: Integer): Boolean
  577.     Clr.w            -(SP)
  578.     Pea            DtxFont(A5)            ; Load the font's number address
  579.     Pea            DtxFace(A5)            ; Load the font's face address
  580.     Pea            DtxSize(A5)            ; Load the font's size address
  581.     Jsr                FontDialog
  582.     
  583. ; Test to see if the okButton was pressed (ie install the new Font variables).
  584. ; If the cancelButton was pressed leave the Font variables as they are.
  585.     Move.b            (SP)+,D0
  586.     Beq.s            @2
  587.     
  588. ; Update the Font number, size, and face fields in the textedit record.  The
  589. ; teAscent and teLineHite fields also require up dating.  Reset the port to 
  590. ; the textedit window, since the current  GraphPort has been changed by
  591. ;  the FontDialog routine.
  592.     
  593.     ; Procedure     SetPort (gp: GraphPort)
  594.     Move.l            WindowPReg,-(SP)    
  595.     _SetPort
  596.  
  597.     ; Procedure TextFont (font: Integer)
  598.     Move.w        DtxFont(A5),-(SP)
  599.     _TextFont
  600.     
  601.     ; Procedure TextFace (face: Style)
  602.     Move.w        DtxFace(A5),-(SP)
  603.     _TextFace
  604.     
  605.     ; Procedure TextSize (size: Integer)
  606.     Move.w        DtxSize(A5),-(SP)
  607.     _TextSize
  608.     
  609.     ; Procedure GetFontInfo (Var Info: FontInfo)
  610.     Pea            Info(A5)
  611.     _GetFontInfo
  612.     
  613.     Movea.l        TextHReg,A1                    ; DeReference the Handle
  614.     Movea.l        (A1),A1
  615.     Move.w        DtxFont(A5),teFont(A1)        ; teFont
  616.     Move.b            DtxFace+1(A5),teFace(A1)        ; teFace
  617.     Move.w        DtxSize(A5),teSize(A1)        ; teSize
  618.     Move.w        Info+ascent(A5),teAscent(A1)    ; teAscent
  619.     Move.w        Info+ascent(A5),D0            ; teLineHite
  620.     Add.w            Info+descent(A5),D0
  621.     Add.w            Info+leading(A5),D0
  622.     Move.w        D0,teLineHite(A1)
  623.         
  624. @2    Bra         GoSetOurPort        
  625.  
  626. InAppleMenu
  627.  
  628. ; It was in the Apple menu.  If it wasn't About, then it must have been a 
  629. ; desk accessory.  If so, open the desk accessory.  
  630.  
  631.     Cmp            #AboutItem,MenuItemReg    ; Is It About?
  632.     Beq             About                    ; If So Goto About...
  633.     
  634.     ; Procedure    GetItem (menu: MenuHandle; item: Integer; 
  635.     ;                        Var itemString: Str255);
  636.     Move.l            AppleHReg,-(SP)        ; Look in Apple Menu
  637.     Move             MenuItemReg,-(SP)    ; What Item Number?
  638.     Pea            DeskName                ; Get Item Name
  639.     _GetItem                                ; Get Item
  640.  
  641.     ; Function    OpenDeskAcc (theAcc: Str255) : Integer;
  642.     Clr                 -(SP)                    ; Space For Opening Result
  643.     Pea            DeskName                ; Open Desk Acc
  644.     _OpenDeskAcc                        ; Open It
  645.     Move             (SP)+,D0                ; Pop result
  646.     
  647. GoSetOurPort
  648.  
  649.     Bsr             SetOurPort                ; Set port to us 
  650.     Bra                ChoiceReturn            ; Unhilite menu and return
  651.                         
  652. ;--------------------------- Text Editing Routines ------------------------
  653.  
  654. Cut                        ; CUT
  655.  
  656.     ; Procedure    TECut (hTE: TEHandle);
  657.     Move.l            TextHReg,-(SP)        ; Identify Text
  658.     _TECut                                    ; Cut it and copy it
  659.     Bra             ChoiceReturn            ; Go get next event
  660.  
  661. Copy                    ; COPY
  662.  
  663.     ; Procedure    TECopy (hTE: TEHandle);
  664.     Move.l            TextHReg,-(SP)        ; Identify Text
  665.     _TECopy                                ; Copy text to clipboard
  666.     Bra             ChoiceReturn            ; Go get next event
  667.  
  668. Paste                    ; PASTE
  669.  
  670.     ; Procedure    TEPaste (hTE: TEHandle);
  671.     Move.l            TextHReg,-(SP)        ; Identify Text
  672.     _TEPaste                                ; Paste
  673.     Bra             ChoiceReturn            ; Go get next event
  674.  
  675. ClearIt                    ;CLEAR
  676.  
  677.     ; Procedure    TEDelete (hTE: TEHandle);
  678.     Move.l            TextHReg,-(SP)        ; Point to text
  679.     _TEDelete                                ; Clear without copying
  680.     Bra             ChoiceReturn            ; Go get next event
  681.  
  682. ; SystemEdit does undo, cut, copy, paste, and clear for desk accessories.
  683. ; It returns False (Beq) if the active window doesn't belong to a
  684. ; desk accessory.
  685.  
  686. SystemEdit
  687.     
  688.     ; Function    SystemEdit (editCmd:Integer): Boolean;
  689.     Clr                 -(SP)                    ; Space for result
  690.     Move            MenuItemReg,-(SP)    ; Get item in Edit menu
  691.     Subq            #1,(SP)                ; SystemEdit is off by 1
  692.     _SysEdit                                ; Do It 
  693.     Move.b         (SP)+,D0                ; Pop result
  694.     Rts                                     ; Beq if NOT handled
  695.  
  696. UnhiliteMenu
  697.     
  698.     ; Procedure    HiLiteMenu (menuID: Integer);
  699.     Clr                -(SP)                    ; All Menus
  700.     _HiLiteMenu                            ; UnHilite Them All
  701.     Rts
  702.     
  703.  
  704. ;---------------------------------Misc Routines---------------------------
  705.  
  706. About
  707.  
  708. ; Call GetNewDialog to read the dialog box parameters from the resource file
  709. ; and display the box.  Set the port to the box, then wait for the proper 
  710. ; click or keypress.  Finally, close the dialog box and set the pointer to us.
  711.  
  712.     ; Function    GetNewDialog (dialogID: Integer; dStorage: Ptr; 
  713.     ;                             behind: WindowPtr) : DialogPtr
  714.     Clr.l            -(SP)                    ; Space For dialog pointer
  715.     Move             #AboutDialog,-(SP)        ; Identify dialog rsrc #
  716.     Pea            DStorage                ; Storage area
  717.     Move.l            #-1,-(SP)                ; Dialog goes on top
  718.     _GetNewDialog                            ; Display dialog box
  719.     Move.l            (SP),-(SP)                ; Copy handle for Close
  720.  
  721.     ; Procedure    SetPort (gp: GrafPort)    ; Handle already on stack
  722.     _SetPort                                ; Make dialog box the port
  723.  
  724.     ; Procedure    TEDeActivate (hTE: TEHandle)
  725.     Move.l            TextHReg,-(SP)        ; Identify Text
  726.     _TEDeActivate                            ; Deactivate Text
  727.  
  728. WaitOK
  729.  
  730.     ; Procedure    ModalDialog (filterProc: ProcPtr; 
  731.     ;                 Var itemHit: Integer);
  732.     Clr.l            -(SP)                    ; Clear space For handle
  733.     Pea            ItemHit                    ; Storage for item hit
  734.     _ModalDialog                            ; Wait for a response
  735.  
  736.     Move            ItemHit,D0                ; Look to see what was hit
  737.     Cmp            #ButtonItem,D0        ; was it OK?
  738.     Bne              WaitOK                ; No, wait for OK
  739.     
  740.     ; Procedure    CloseDialog (theDialog: DialogPtr);
  741.     _CloseDialog                            ; Handle already on stack
  742.      Bra            GoSetOurPort            ; Set port to us and return
  743.      
  744. ; ---------------------------- Data Starts Here -------------------------
  745.  
  746. EventRecord                                    ; NextEvent's Record
  747.  What:                 Dc             0                ; Event number
  748.  Message:            Dc.l        0                ; Additional information
  749.  When:                Dc.l        0                ; Time event was posted 
  750.  Point:                Dc.l        0                ; Mouse coordinates
  751.  Modify:            Dc             0                ; State of keys and button
  752.  WWindow:            Dc.l        0                ; Find Window's Result
  753.  
  754. DStorage            Dcb.w        DWindLen,0    ; Storage For Dialog
  755. DeskName            Dcb.w        16,0            ; Desk Accessory's Name
  756. WBounds            Dc             28,4,308,508    ; Drag Window's Bounds
  757. ViewRect            Dc             5,4,245,405    ; Text Record's View Rect
  758. DestRect            Dc             5,4,245,405    ; Text Record's Dest Rect
  759. ItemHit                Dc            0                ; Item clicked in dialog
  760.  
  761. ;------------------------- Nonrelocatable Storage -----------------------
  762.  
  763. ; Variables declared using Ds are placed in a global space relative to
  764. ; A5.  When these variables are referenced, A5 must be explicitly mentioned.
  765.  
  766. DtxFont            Ds.w        1
  767. DtxFace            Ds.w        1
  768. DtxSize            Ds.w        1
  769. Info                Ds.w        4                ; FontInfo Record
  770. WindowStorage    Ds.w        WindowSize    ; Storage for Window
  771.  
  772. End
  773.